<template>
  <a-drawer
    title="编辑"
    width="40%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="组织编码" prop="DeptCode">
          <a-input v-model="entity.DeptCode" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="组织名称" prop="Name">
          <a-input v-model="entity.Name" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="组织全称" prop="FullName">
          <a-input v-model="entity.FullName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="上级组织" prop="ParentId">
          <a-tree-select
            v-model="entity.ParentId"
            allowClear
            :treeData="ParentIdTreeData"
            placeholder="请选择上级组织"
            treeDefaultExpandAll
          ></a-tree-select>
        </a-form-model-item>
        <a-form-model-item label="组织类型" prop="Type">
          <a-select v-model="entity.Type" style="width: 100%">
            <a-select-option v-for="item in TypeList" :key="item.Id">
              {{ item.TypeName }}
            </a-select-option>
          </a-select>
        </a-form-model-item>

        <a-form-model-item label="法人姓名" prop="Corporation">
          <a-input v-model="entity.Corporation" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="组织地址" prop="Address">
          <a-input v-model="entity.Address" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="排序" prop="Sort">
          <a-input v-model="entity.Sort" type="number" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="纬度" prop="Plantlatitude">
          <a-input v-model="entity.Plantlatitude" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="经度" prop="Plantlongitude">
          <a-input v-model="entity.Plantlongitude" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="宽度" prop="Plantwidth">
          <a-input v-model="entity.Plantwidth" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="长度" prop="Plantlength">
          <a-input v-model="entity.Plantlength" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="高度" prop="PlantlHeight">
          <a-input v-model="entity.PlantlHeight" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="传真" prop="Factoryfax  ">
          <a-input v-model="entity.Factoryfax" autocomplete="off" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '10px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1
      }"
    >
      <a-button
        :style="{ marginRight: '8px' }"
        @click="
          () => {
            this.visible = false
          }
        "
      >取消</a-button
      >
      <a-button type="primary" @click="handleSubmit">保存</a-button>
    </div>
  </a-drawer>
</template>

<script>
export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    }
  },
  data () {
    return {
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      TypeList: [],
      confirmLoading: false,
      entity: {},
      ParentIdTreeData: [],
      rules: {
        Name: [{ required: true, message: '必填' }]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })

      this.$http.post('/Base_Manage/Base_Department/GetTreeDataList', {}).then(resJson => {
        if (resJson.Success) {
          this.ParentIdTreeData = resJson.Data
        }
      })
    },
    openForm (id) {
      this.init()

      this.$http.post('/Base_Manage/Base_DepartmentType/GetDataList', {}).then(resJson => {
        this.TypeList = resJson.Data
      })

      if (id) {
        this.$http.post('/Base_Manage/Base_Department/GetTheData', { id: id }).then(resJson => {
          this.entity = resJson.Data
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.confirmLoading = true
        this.$http.post('/Base_Manage/Base_Department/SaveData', this.entity).then(resJson => {
          this.confirmLoading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.afterSubmit()
            this.visible = false
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
